package com.elegantsoft.pakistannews;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockListActivity;
import com.atiffarrukh.pakistannews.R;

public class RSSFeed extends SherlockListActivity {
	
	
	// All static variables
	static final String URL = "http://api.androidhive.info/pizza/?format=xml";
	// XML node keys
	static final String KEY_ITEM = "item"; // parent node
	static final String KEY_ID = "id";
	static final String KEY_NAME = "name";
	static final String KEY_COST = "cost";
	static final String KEY_DESC = "description";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view);
		Bundle extras = getIntent().getExtras();
		
		String cat,chan,a,b;
		
		cat = extras.getString("category");
		chan = extras.getString("channelName");
		//Toast.makeText(getApplicationContext(),"Category : " + cat + " and Channel :" + chan, Toast.LENGTH_SHORT).show();
	
		ArrayList<HashMap<String, String>> rssList = new ArrayList<HashMap<String,String>>();
		
		XMLParser parser = new XMLParser();
		String xml = parser.getXmlFromUrl(URL);
		Document doc = parser.getDomElement(xml); 
		
		NodeList nl =  doc.getElementsByTagName(KEY_ID);
		// looping through all item nodes <item>
				for (int i = 0; i < nl.getLength(); i++) {
					// creating new HashMap
					HashMap<String, String> map = new HashMap<String, String>();
					Element e = (Element) nl.item(i);
					// adding each child node to HashMap key => value
					map.put(KEY_ID, parser.getValue(e, KEY_ID));
					map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
					map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
					map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

					// adding HashList to ArrayList
					rssList.add(map);
				}
				// Adding menuItems to ListView
				
				ListAdapter adapter = new SimpleAdapter(this, rssList,
										R.layout.rss_list, new String[]{ KEY_NAME, KEY_DESC}, 
										new int[]{R.id.Title,R.id.description});
				setListAdapter(adapter);
				Log.d("Title", KEY_NAME);
				Log.d("description", KEY_DESC);
				ListView lv = getListView();
	}
}
